package com.example.demo.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.annotation.PostConstruct;
import lombok.SneakyThrows;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
public class EventController {
    @Value("${new-group-member-welcome-message}")
    private String newGroupMemberWelcomeMessage;
    private OkHttpClient client = new OkHttpClient();
    @Value("${base-url}")
    private String baseUrl;
    @Value("${group-id}")
    private String groupId;
    @Autowired
    private ObjectMapper objectMapper;

    @PostMapping("/event")
    public void event(@RequestBody JsonNode jsonNode) {
        String postType = jsonNode.get("post_type").asText();
        if (postType.equals("notice")) {
            String noticeType = jsonNode.get("notice_type").asText();
            if (noticeType.equals("group_increase")) {
                groupMemberIncrease(jsonNode);
            }
        } else if (postType.equals("request")) {
            String requestType = jsonNode.get("request_type").asText();
            if (requestType.equals("group") && jsonNode.get("sub_type").asText().equals("add")) {
                requestJoinGroup(jsonNode);
            }
        }
    }

    @SneakyThrows
    public void groupMemberIncrease(JsonNode jsonNode) {
        if (!jsonNode.get("group_id").asText().equals(groupId)) {
            return;
        }
        ObjectNode objectNode = objectMapper.createObjectNode();
        objectNode.put("group_id", groupId);
        objectNode.put("message", "[CQ:at,qq=%s]\n%s".formatted(jsonNode.get("user_id").asText(), newGroupMemberWelcomeMessage));

        Request request = new Request.Builder()
                .post(okhttp3.RequestBody.create(MediaType.parse("utf-8"), objectNode.toString()))
                .header("Content-Type", "application/json")
                .url(baseUrl + "/send_group_msg")
                .build();
        client.newCall(request).execute();
    }

    @SneakyThrows
    public void requestJoinGroup(JsonNode jsonNode) {
        Request request = new Request.Builder()
                .post(okhttp3.RequestBody.create(MediaType.parse("utf-8"), """
                {
                    "flag": "%s",
                    "type": "add",
                    "approve": true
                }
                """.formatted(jsonNode.get("flag").asText())))
                .header("Content-Type", "application/json")
                .url(baseUrl + "/set_group_add_request")
                .build();
        client.newCall(request).execute();
    }
}
